package com.yonyou.byb.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.comn.NetStreamContext;
import nc.bs.framework.server.ISecurityTokenCallback;
import nc.bs.logging.Logger;

import com.yonyou.byb.BybConstant;
import com.yonyou.byb.ConfigContext;

@SuppressWarnings("restriction")
public class BybFilter implements  Filter{

	@Override
	public void init(FilterConfig paramFilterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		Logger.init(BybConstant.logModule);
		Logger.error(((HttpServletRequest)req).getRequestURI());
		
		login();
		chain.doFilter(req, resp);
	}
	
	private void login() {
		InvocationInfoProxy.getInstance().setUserDataSource(ConfigContext.getInstance().getDatasource());
		InvocationInfoProxy.getInstance().setUserCode(BybConstant.BYB_USER_CODE);
		InvocationInfoProxy.getInstance().setUserId(BybConstant.BYB_USER_ID);
		InvocationInfoProxy.getInstance().setGroupId(BybConstant.GROUP_ID);
		
		ISecurityTokenCallback tc = NCLocator.getInstance().lookup(ISecurityTokenCallback.class);
		byte[] token = tc.token("__system".getBytes(), "admin".getBytes());
		NetStreamContext.setToken(token);
	}

	@Override
	public void destroy() {
	}

}
